package com.wicket.okrfinance.processor.mqdemo.daemon;


import com.wicket.okrfinance.biz.service.EvaCalcService;
import com.wicket.okrfinance.biz.service.dto.req.ImplementMqLongevityGuardianComReqDto;
import com.wicket.okrfinance.biz.service.dto.req.ImplementRedisLongevityGuardianComReqDto;
import com.wicket.okrfinance.common.util.SpringContextUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class LongevityGuard implements DisposableBean,Runnable {


    private Thread thread;




    private volatile boolean someCondition = true;



    // dao 和service注入
    @Autowired
    public LongevityGuard() {
        this.thread = new Thread(this);
        this.thread.start();
        System.out.print("财务服务长生守护启动\n");
    }

    @Override
    public void run() {
        while (someCondition) {
            EvaCalcService service = SpringContextUtil.getBean(EvaCalcService.class);
            try {
                System.out.print("MQ长生守护一次执行开始\n");
                ImplementMqLongevityGuardianComReqDto mqReq = new ImplementMqLongevityGuardianComReqDto();
                service.implementMqLongevityGuardianCom(mqReq);
                System.out.print("MQ长生守护一次执行结束\n");

            } catch (Exception e){
                e.printStackTrace();
                System.out.print("【异常】MQ长生守护一次执行异常结束\n");
            }

            try{
                System.out.print("redis长生守护一次执行开始\n");
                ImplementRedisLongevityGuardianComReqDto req = new ImplementRedisLongevityGuardianComReqDto();
                service.implementRedisLongevityGuardianCom(req);
                System.out.print("redis长生守护一次执行结束\n");
            } catch (Exception e){
                e.printStackTrace();
                System.out.print("【异常】redis长生守护一次执行异常结束\n");
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }




    @Override
    public void destroy() throws Exception {
        someCondition = false;
    }
}
